import axios from "axios";
import Cookie from "js-cookie";

const request = axios.create({
  timeout: 3000,
  timeoutErrorMessage: "请求超时",
});

// 请求拦截 前端 -> 拦截 -> 后端
request.interceptors.request.use(
  (config) => {
    // 添加公共参数
    const token = Cookie.get("token");

    if (token) {
      return {
        ...config,
        authorization: token,
      };
    }

    return config;
  },
  (err) => {
    // 如果拦截发生错误 返回失败
    return err
  }
);

// 响应拦截 后端 -> 拦截 -> 前端
request.interceptors.response.use(
  (config) => {
    return config.data;
  },
  (err) => {
    return err
  }
);


export default request;
